package com.kkwrite.regimen.provider.service.join;

import com.kkwrite.regimen.common.constant.DeletedEnum;
import com.kkwrite.regimen.common.entity.dto.apply.ApplyJoinInDTO;
import com.kkwrite.regimen.provider.domain.RegApplyJoinDO;
import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.repository.RegApplyJoinRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;

/**
 * 申请加盟服务
 * @author Ke.Wang
 * @date 2018年7月16日 下午10:40:51
 */
@Service("joinService")
public class JoinServiceImpl implements JoinService {
	
	private static final Logger logger = LoggerFactory.getLogger(JoinServiceImpl.class);

	@Resource
	private RegApplyJoinRepository regApplyJoinRepository;
	
	@Override
	public void saveApply(ApplyJoinInDTO applyJoinInDTO) throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("JoinServiceImpl.saveApply(), applyJoinInDTO = {}", applyJoinInDTO);
		}
		
		RegApplyJoinDO regApplyJoinDO = new RegApplyJoinDO();
		BeanUtils.copyProperties(applyJoinInDTO, regApplyJoinDO);
		// TODO 需要
		regApplyJoinDO.setApplyJoinStatus((short) 1);
		regApplyJoinDO.setDeleted(DeletedEnum.UN_DELETED.getValue());
		regApplyJoinDO.setGmtCreate(new Timestamp(System.currentTimeMillis()));
		regApplyJoinDO.setGmtModified(new Timestamp(System.currentTimeMillis()));
		
		try {
			regApplyJoinDO = regApplyJoinRepository.saveAndFlush(regApplyJoinDO);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}

}
